#!/usr/bin/env python

# _*_ coding:utf-8 _*_
import tkinter as tk
from tkinter import ttk
import tkinter.font as tf
import sqliteDemo
    

# 数据库连接
sqlite_db = sqliteDemo.SqliteDB()
# 目前去最大值进行更新Update操作
rowid=sqlite_db.get_max_rowid()
print(rowid)

'''紧耦合'''

# 弹窗
class OpenDialog(tk.Toplevel):
    def __init__(self, parent):
        super().__init__()
        self.title('outLine List')
        self.parent = parent # 显式地保留父窗口

        # 第三行
        # row3 = tk.Frame(self)
        # row3.pack(fill="x")
        # btn_list=tk.Button(row3, text="2020-12-07 14:35:50", 
        #     height = 5, 
        #     width = 40,
        #     command=self.seleteOne).pack()
        # btn_list.place(x=10,y=10)
        tree=ttk.Treeview(self,show = "headings",selectmode = tk.BROWSE)#表格
        tree["columns"]=("date","text")
        tree.column("date",width=150) #表示列,不显示
        tree.column("text",width=300)
        tree.heading("date",text="时  间") #显示表头
        tree.heading("text",text="内  容")
        text_arr= sqlite_db.get_by_rowid(rowid)
        tree.insert("",1,values=(text_arr[1],text_arr[2]))
        # tree.insert("",2,text="line1" ,values=("1","2","3"))
        # tree.insert("",3,text="line1" ,values=("1","2","3"))
        tree.bind("<Double-1>", self.seleteOne)
        tree.pack()
    # 选择一行
    def seleteOne(self, event):
        curItem = self.tree.focus()
        print(self.tree.item(curItem))
        # 显式地更新父窗口界面
        self.parent.l1.config(text=self.parent.name)
        self.parent.l2.config(text=self.parent.age)
        self.destroy() # 销毁窗口
# 主窗
class mainOutline(tk.Tk):
    def __init__(self):
        super().__init__()
        # self.pack() # 若继承 tk.Frame，此句必须有！！！
        self.title('便签')
        # 程序界面
        self.mainUI()
    def mainUI(self):
        self.geometry('500x640+500+100')
        self.bind('<Control-s>', self.key)

        #1、输入框
        e_text =tk.Text(self)
        # width:长度；height:高度
        e_text.place(width=500, height=600)  
        # e_text.grid(row=10,column=10,sticky=E)
        e_text.insert(tk.END,sqlite_db.get_by_rowid(rowid)[2])

        #2、打开按钮
        btn_open = tk.Button(self,text='Open',command=self.open)
        btn_open.place(x=10,y=605)
   

    # 按扭调用的函数,改为ctrl+s
    def reg():
        global rowid
        text = e_text.get('0.0','end')
        rowid=sqlite_db.update(rowid,text)
        print("更新id %s 的内容 "%rowid)
        text_arr =text.splitlines(True)
     # 捕捉快捷键
    def key(event):
        self.reg()
        
    def open(self):
        "open"
        pw = OpenDialog(self)
        self.wait_window(pw) # 这一句很重要！！！
if __name__ == '__main__':
    app = mainOutline()
    app.mainloop()

